package com.jn.viewtest_2.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class MyLayout extends ViewGroup {
   public MyLayout(Context context) {
      super(context);
   }

   public MyLayout(Context context, AttributeSet attrs) {
      super(context, attrs);
   }

   @Override
   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
      int widthMode = MeasureSpec.getMode(widthMeasureSpec);
      int widthSize = MeasureSpec.getSize(widthMeasureSpec);
      int heightMode = MeasureSpec.getMode(heightMeasureSpec);
      int heightSize = MeasureSpec.getSize(heightMeasureSpec);
      measureChildren(widthMeasureSpec, heightMeasureSpec);
      int measuredWidth = 0;
      int measuredHeight = 0;
      int count = getChildCount();
      switch (widthMode) {
         case MeasureSpec.AT_MOST:
            for (int i = 0; i < count; i++) {
               measuredWidth += getChildAt(i).getMeasuredWidth();
            }
            break;
         case MeasureSpec.EXACTLY:
         case MeasureSpec.UNSPECIFIED:
            measuredWidth = widthSize;
            break;
      }
      switch (heightMode) {
         case MeasureSpec.AT_MOST:
            for (int i = 0; i < count; i++) {
               measuredHeight += getChildAt(i).getMeasuredHeight();
            }
            break;
         case MeasureSpec.EXACTLY:
         case MeasureSpec.UNSPECIFIED:
            measuredHeight = heightSize;
            break;
      }
      setMeasuredDimension(measuredWidth, measuredHeight);
   }

   @Override
   protected void onLayout(boolean changed, int l, int t, int r, int b) {
      int childCount = getChildCount();
      int left = 0;
      int top = 0;
      View child;
      for (int i = 0; i < childCount; i++) {
         child = getChildAt(i);
         if (child.getVisibility() != View.GONE) {
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();
            child.layout(left, top, left + childWidth, top + childHeight);
            left += childWidth/1.3;
            top += childHeight/1.3;
         }
      }
   }
}
